package com.luxus.dev;

import java.util.ArrayList;
import java.util.List;

import com.android.camera.debug.Log;
import com.android.camera.one.CaptureRequestExtend;
import com.android.camera.one.CaptureResultExtend;
import com.android.camera.one.OneCamera.IMetadataResult;
import com.android.camera.one.OneCamera.MetadataListener;
import com.android.camera.one.v2.camera2proxy.CameraCaptureSessionClosedException;
import com.android.camera.one.v2.core.FrameServer;
import com.android.camera.one.v2.core.Request;
import com.android.camera.one.v2.core.RequestBuilder;
import com.android.camera.one.v2.core.ResourceAcquisitionFailedException;
import com.android.camera.one.v2.imagesaver.ImageSaver;
import com.android.camera.one.v2.photo.ImageCaptureCommandExt;
import com.android.camera.one.v2.photo.PictureCallbackAdapter;

import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.CaptureResult;

public class DebugForCaptureRequest extends ImageCaptureCommandExt
		implements MetadataListener {
	private static final Log.Tag TAG = new Log.Tag("DebugForCap");
	
	@Override
	public void onMetadataCallback(IMetadataResult result) {
		try {
			CaptureResult.Key<Float> focusValueKey = CaptureResultExtend.
					getKey("org.quic.camera.focusvalue.FocusValue", Float.class);
			Log.d(TAG, "hpx focusvalue = "+result.get(focusValueKey));
			CaptureResult.Key<Integer> fvision = CaptureResultExtend.
					getKey("vivo.sensor.info.FVision", Integer.class);
			Log.d(TAG, "hpx fvision = "+result.get(fvision));
			CaptureResult.Key<Integer> confidence = CaptureResultExtend.
					getKey("vivo.3a.info.confidence", Integer.class);
			Log.d(TAG, "hpx confidence = "+result.get(confidence));
			CaptureResult.Key<Integer> phase_diff = CaptureResultExtend.
					getKey("vivo.3a.info.phase_diff", Integer.class);
			Log.d(TAG, "hpx phase_diff = "+result.get(phase_diff));
			CaptureResult.Key<Integer> defocus = CaptureResultExtend.
					getKey("vivo.3a.info.defocus", Integer.class);
			Log.d(TAG, "hpx defocus = "+result.get(defocus));
			
			CaptureResult.Key<Integer> acc = CaptureResultExtend.
					getKey("vivo.ois.calibration.setAcc", Integer.class);
			Log.d(TAG, "liuchangyou acc output = "+result.get(acc));
		} catch (Exception ex) {
			
		}
	}
	
	private int accValue = 0;
	@Override
	public void run(PictureCallbackAdapter pictureCallbackAdapter, ImageSaver imageSaver) throws InterruptedException,
			CameraAccessException, CameraCaptureSessionClosedException, ResourceAcquisitionFailedException {
		FrameServer.Session session = mFrameServer.createExclusiveSession();
		CaptureRequest.Key<Integer> fvision = null;
		Log.d(TAG, "DebugForCaptureRequest E");
		try {
			List<Request> burstRequest1 = new ArrayList<>(1);
			RequestBuilder builder1 = mBuilderFactory.create(CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG);
			fvision = CaptureRequestExtend.
					getKey("vivo.sensor.status.difference", Integer.class);
			builder1.setParam(fvision, 666);
			burstRequest1.add(builder1.build());
			session.submitRequest(burstRequest1, FrameServer.RequestType.NON_REPEATING);
			
			List<Request> burstRequest2 = new ArrayList<>(1);
			RequestBuilder builder2 = mBuilderFactory.create(CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG);
			builder2.setParam(fvision, 0);
			burstRequest2.add(builder2.build());
			session.submitRequest(burstRequest2, FrameServer.RequestType.NON_REPEATING);
			
			List<Request> burstRequest3 = new ArrayList<>(1);
			RequestBuilder builder3 = mBuilderFactory.create(CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG);
			CaptureRequest.Key<Integer> acc = CaptureRequestExtend.
					getKey("vivo.ois.calibration.setAcc", Integer.class);
			int nAccValue = (accValue++) % 2;
			builder3.setParam(acc, nAccValue);
			burstRequest3.add(builder3.build());
			session.submitRequest(burstRequest3, FrameServer.RequestType.NON_REPEATING);
			Log.d(TAG, "liuchangyou acc input = "+nAccValue);
		} catch (Exception ex) {
			Log.d(TAG, "DebugForCaptureRequest "+ex.toString());
        } finally {
        	List<Request> burstRequest = new ArrayList<>(1);
        	RequestBuilder builder = mBuilderFactory.create(CameraDevice.TEMPLATE_ZERO_SHUTTER_LAG);
        	burstRequest.add(builder.build());
			session.submitRequest(burstRequest, FrameServer.RequestType.REPEATING);
            imageSaver.close();
            session.close();
        }
		Log.d(TAG, "DebugForCaptureRequest X");
	}
}
